#ifndef ENTITY3D_H
#define ENTITY3D_H

#include <d3d9.h>
#include <d3dx9.h>

#include <Cg/cg.h>
#include <Cg/cgD3D9.h>

#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")


#pragma comment( lib, "cg.lib" )
#pragma comment( lib, "cgD3D9.lib" )
extern CGparameter mvp;
extern CGparameter modelWorld;
extern CGprogram program;
extern CGprogram programFragment;
extern CGparameter normalTexture;
extern CGprogram postFragment;
extern CGparameter renderTexture;

extern CGprogram dispersionProgram;
extern CGparameter dispersionDecalTexture;
extern LPDIRECT3DTEXTURE9 pSkyboxTexture;
extern CGparameter skyBoxDispersionTexture;

extern CGprogram lightSourceProgram;
extern CGparameter lightSourceTexture;

extern LPDIRECT3DTEXTURE9 pRenderTexture;

struct MeshInfo
{
	LPD3DXMESH mesh;
	DWORD numMaterials;
	D3DMATERIAL9 * meshMaterials;
	LPDIRECT3DTEXTURE9 * meshTextures;
};

struct CUSTOMVERTEX
{
	float x,y,z;	//info sobre posicion
	D3DXVECTOR3 normal;
	float u,v;
	float u1,v1;
};

#define CUSTOMFVF (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEX2|D3DFVF_TEXCOORDSIZE2(2))

class Entity3D
{
protected:
	D3DXVECTOR3 position;
	D3DXVECTOR3 angle;
	D3DXVECTOR3 speed;
	D3DXVECTOR3 acceleration;
	D3DXVECTOR3 scale;
	D3DXVECTOR3 forward;
	D3DXVECTOR3 up;
	D3DXVECTOR3 boundingBox;
	bool isActive;
	float c;
	int add;
	MeshInfo *meshInfo;
public:
	Entity3D();
	Entity3D(D3DXVECTOR3 _position,D3DXVECTOR3 _angle,D3DXVECTOR3 _scale,D3DXVECTOR3 _speed,D3DXVECTOR3 _acceleration,bool _isActive,MeshInfo * _mesh);
	void setPosition(D3DXVECTOR3 _position);
	D3DXVECTOR3 getPosition();
	void setAngle(D3DXVECTOR3 _angle);
	D3DXVECTOR3 getAngle();
	void setScale(D3DXVECTOR3 _scale);
	D3DXVECTOR3 getScale();
	void setSpeed(D3DXVECTOR3 _speed);
	D3DXVECTOR3 getSpeed();
	void setAcceleration(D3DXVECTOR3 _acceleration);
	D3DXVECTOR3 getAcceleration();
	D3DXVECTOR3 getForward();
	D3DXVECTOR3 getUp();
	void setIsActive(bool _isActive);
	bool getIsActive();
	void setMesh(MeshInfo *_mesh);
	D3DXVECTOR3 lookAt(D3DXVECTOR3 _lookAtPosition);
	void calculateBoundingBox();
	MeshInfo * getMesh();
	void Draw(LPDIRECT3DDEVICE9 _d3ddev,bool _light);
	void Draw(LPDIRECT3DDEVICE9 _d3ddev,LPDIRECT3DVERTEXBUFFER9 _v_buffer,LPDIRECT3DINDEXBUFFER9 _i_buffer);
	void Draw(LPDIRECT3DDEVICE9 _d3ddev,LPDIRECT3DVERTEXBUFFER9 _v_buffer,LPDIRECT3DINDEXBUFFER9 _i_buffer,int _cantVertex,int _cantPrimitives);
	virtual void Update(DWORD dt);
	D3DXVECTOR3 getBoundingBox();
	bool CheckCollision(Entity3D * _collisionObject);
};

#endif